//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tencent.bugly.crashreport.crash.anr;

import android.app.ActivityManager;
import android.app.ActivityManager.ProcessErrorStateInfo;
import android.content.Context;
import android.os.FileObserver;
import android.os.Looper;
import android.os.Process;
import android.text.TextUtils;
import com.tencent.bugly.crashreport.common.info.a;
import com.tencent.bugly.crashreport.crash.CrashDetailBean;
import com.tencent.bugly.crashreport.crash.c;
import com.tencent.bugly.proguard.aa;
import com.tencent.bugly.proguard.ab;
import com.tencent.bugly.proguard.ac;
import com.tencent.bugly.proguard.w;
import com.tencent.bugly.proguard.x;
import com.tencent.bugly.proguard.y;
import com.tencent.bugly.proguard.z;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;

public final class b implements ac {
    private AtomicInteger a = new AtomicInteger(0);
    private long b = -1L;
    private final Context c;
    private final a d;
    private final w e;
    private final com.tencent.bugly.crashreport.common.strategy.a f;
    private final String g;
    private final com.tencent.bugly.crashreport.crash.b h;
    private FileObserver i;
    private boolean j = true;
    private ab k;
    private int l;
    private ProcessErrorStateInfo m;

    public b(Context var1, com.tencent.bugly.crashreport.common.strategy.a var2, a var3, w var4, com.tencent.bugly.crashreport.crash.b var5) {
        this.c = z.a(var1);
        File var6 = var1.getDir("bugly", 0);
        this.g = var6.getAbsolutePath();
        this.d = var3;
        this.e = var4;
        this.f = var2;
        this.h = var5;
        this.m = new ProcessErrorStateInfo();
    }

    private ProcessErrorStateInfo a(Context var1, long var2) {
        try {
            var2 = Math.max(10000L, 0L);
            x.c("to find!");
            ActivityManager var9 = (ActivityManager)var1.getSystemService(Context.ACTIVITY_SERVICE);
            long var5 = var2 / 500L;
            int var10 = 0;

            do {
                x.c("waiting!");
                List var3;
                if ((var3 = var9.getProcessesInErrorState()) != null) {
                    Iterator var11 = var3.iterator();

                    while(var11.hasNext()) {
                        ProcessErrorStateInfo var4;
                        if ((var4 = (ProcessErrorStateInfo)var11.next()).condition == 2) {
                            x.c("found!");
                            return var4;
                        }
                    }
                }

                z.b(500L);
            } while((long)(var10++) < var5);

            x.c("end!");
        } catch (Exception var7) {
            x.b(var7);
        } catch (OutOfMemoryError var8) {
            this.m.pid = Process.myPid();
            this.m.shortMsg = "bugly sdk waitForAnrProcessStateChanged encount error:" + var8.getMessage();
            return this.m;
        }

        return null;
    }

    private CrashDetailBean a(com.tencent.bugly.crashreport.crash.anr.a var1) {
        CrashDetailBean var2 = new CrashDetailBean();

        try {
            var2.C = com.tencent.bugly.crashreport.common.info.b.k();
            var2.D = com.tencent.bugly.crashreport.common.info.b.i();
            var2.E = com.tencent.bugly.crashreport.common.info.b.m();
            var2.F = this.d.p();
            var2.G = this.d.o();
            var2.H = this.d.q();
            if (!com.tencent.bugly.crashreport.common.info.b.t()) {
                var2.w = z.a(this.c, com.tencent.bugly.crashreport.crash.c.e, null);
            }

            var2.b = 3;
            var2.e = this.d.h();
            var2.f = this.d.k;
            var2.g = this.d.w();
            var2.m = this.d.g();
            var2.n = "ANR_EXCEPTION";
            var2.o = var1.f;
            var2.q = var1.g;
            var2.P = new HashMap<>();
            var2.P.put("BUGLY_CR_01", var1.e);
            int var3 = -1;
            if (var2.q != null) {
                var3 = var2.q.indexOf("\n");
            }

            var2.p = var3 > 0 ? var2.q.substring(0, var3) : "GET_FAIL";
            var2.r = var1.c;
            if (var2.q != null) {
                var2.u = z.b(var2.q.getBytes());
            }

            var2.z = var1.b;
            var2.A = var1.a;
            var2.B = "main(1)";
            var2.I = this.d.y();
            var2.h = this.d.v();
            var2.i = this.d.J();
            var2.v = var1.d;
            var2.L = this.d.o;
            var2.M = this.d.a;
            var2.N = this.d.a();
            var2.Q = this.d.H();
            var2.R = this.d.I();
            var2.S = this.d.B();
            var2.T = this.d.G();
            if (!com.tencent.bugly.crashreport.common.info.b.t()) {
                this.h.d(var2);
            }

            var2.y = y.a();
        } catch (Throwable var4) {
            if (!x.a(var4)) {
                var4.printStackTrace();
            }
        }

        return var2;
    }

    private static boolean a(String var0, String var1, String var2) {
        com.tencent.bugly.crashreport.crash.anr.TraceFileHelper.a var19;
        if ((var19 = TraceFileHelper.readTargetDumpInfo(var2, var0, true)) != null && var19.d != null && var19.d.size() > 0) {
            File var21 = new File(var1);

            try {
                if (!var21.exists()) {
                    if (!var21.getParentFile().exists()) {
                        var21.getParentFile().mkdirs();
                    }

                    var21.createNewFile();
                }
            } catch (Exception var13) {
                if (!x.a(var13)) {
                    var13.printStackTrace();
                }

                x.e("backup file create error! %s  %s", var13.getClass().getName() + ":" + var13.getMessage(), var1);
                return false;
            }

            if (var21.exists() && var21.canWrite()) {
                BufferedWriter var3 = null;
                boolean var12 = false;

                label225: {
                    try {
                        var12 = true;
                        var3 = new BufferedWriter(new FileWriter(var21, false));
                        String[] var20;
                        if ((var20 = (String[])var19.d.get("main")) != null && var20.length >= 3) {
                            var2 = var20[0];
                            String var4 = var20[1];
                            var1 = var20[2];
                            var3.write("\"main\" tid=" + var1 + " :\n" + var2 + "\n" + var4 + "\n\n");
                            var3.flush();
                        }

                        Iterator var22 = var19.d.entrySet().iterator();

                        while(var22.hasNext()) {
                            Entry var23;
                            if (!((String)(var23 = (Entry)var22.next()).getKey()).equals("main") && var23.getValue() != null && ((String[])var23.getValue()).length >= 3) {
                                var1 = ((String[])var23.getValue())[0];
                                var0 = ((String[])var23.getValue())[1];
                                String var5 = ((String[])var23.getValue())[2];
                                var3.write("\"" + (String)var23.getKey() + "\" tid=" + var5 + " :\n" + var1 + "\n" + var0 + "\n\n");
                                var3.flush();
                            }
                        }

                        var12 = false;
                        break label225;
                    } catch (IOException var17) {
                        if (!x.a(var17)) {
                            var17.printStackTrace();
                        }

                        x.e("dump trace fail %s", new Object[]{var17.getClass().getName() + ":" + var17.getMessage()});
                        var12 = false;
                    } finally {
                        if (var12) {
                            if (var3 != null) {
                                try {
                                    var3.close();
                                } catch (IOException var15) {
                                    if (!x.a(var15)) {
                                        var15.printStackTrace();
                                    }
                                }
                            }

                        }
                    }

                    if (var3 != null) {
                        try {
                            var3.close();
                        } catch (IOException var14) {
                            if (!x.a(var14)) {
                                var14.printStackTrace();
                            }
                        }
                    }

                    return false;
                }

                try {
                    var3.close();
                } catch (IOException var16) {
                    if (!x.a(var16)) {
                        var16.printStackTrace();
                    }
                }

                return true;
            } else {
                x.e("backup file create fail %s", new Object[]{var1});
                return false;
            }
        } else {
            x.e("not found trace dump for %s", new Object[]{var2});
            return false;
        }
    }

    public final boolean a() {
        return this.a.get() != 0;
    }

    private boolean a(Context var1, String var2, ProcessErrorStateInfo var3, long var4, Map<String, String> var6) {
        long var10002 = var4;
        Map var19 = var6;
        long var13 = var10002;
        File var15 = new File(var1.getFilesDir(), "bugly/bugly_trace_" + var13 + ".txt");
        com.tencent.bugly.crashreport.crash.anr.a var5;
        (var5 = new com.tencent.bugly.crashreport.crash.anr.a()).c = var13;
        var5.d = var15.getAbsolutePath();
        var5.a = var3 != null ? var3.processName : "";
        var5.f = var3 != null ? var3.shortMsg : "";
        var5.e = var3 != null ? var3.longMsg : "";
        var5.b = var6;
        Thread var16 = Looper.getMainLooper().getThread();
        if (var6 != null) {
            Iterator var17 = var6.keySet().iterator();

            while(var17.hasNext()) {
                String var20;
                if ((var20 = (String)var17.next()).startsWith(var16.getName())) {
                    var5.g = (String)var19.get(var20);
                    break;
                }
            }
        }

        if (TextUtils.isEmpty(var5.g)) {
            var5.g = "main stack is null , some error may be encountered.";
        }

        x.c("anr tm:%d\ntr:%s\nproc:%s\nmain stack:%s\nsMsg:%s\n lMsg:%s\n threads:%d", new Object[]{var5.c, var5.d, var5.a, var5.g, var5.f, var5.e, var5.b == null ? 0 : var5.b.size()});
        if (!this.f.c().j) {
            x.d("ANR Report is closed! print local for helpful!", new Object[0]);
            com.tencent.bugly.crashreport.crash.b.a("ANR", z.a(), var5.a, "main", var5.g, (CrashDetailBean)null);
            return false;
        } else {
            x.a("found visiable anr , start to upload!", new Object[0]);
            CrashDetailBean var18;
            if ((var18 = this.a(var5)) == null) {
                x.e("pack anr fail!", new Object[0]);
                return false;
            } else {
                com.tencent.bugly.crashreport.crash.c.a().a(var18);
                if (var18.a >= 0L) {
                    x.a("backup anr record success!", new Object[0]);
                } else {
                    x.d("backup anr record fail!", new Object[0]);
                }

                if (var2 != null && (new File(var2)).exists()) {
                    this.a.set(3);
                    if (a(var2, var5.d, var5.a)) {
                        x.a("backup trace success", new Object[0]);
                    }
                }

                com.tencent.bugly.crashreport.crash.b.a("ANR", z.a(), var5.a, "main", var5.g, var18);
                if (!this.h.a(var18)) {
                    this.h.a(var18, 3000L, true);
                }

                this.h.c(var18);
                return true;
            }
        }
    }

    public final void a(String var1) {
        synchronized(this) {
            if (this.a.get() != 0) {
                x.c("trace started return ", new Object[0]);
                return;
            }

            this.a.set(1);
        }

        try {
            x.c("read trace first dump for create time!", new Object[0]);
            long var2 = -1L;
            com.tencent.bugly.crashreport.crash.anr.TraceFileHelper.a var4;
            if ((var4 = TraceFileHelper.readFirstDumpInfo(var1, false)) != null) {
                var2 = var4.c;
            }

            if (var2 == -1L) {
                x.d("trace dump fail could not get time!", new Object[0]);
                var2 = System.currentTimeMillis();
            }

            if (Math.abs(var2 - this.b) < 10000L) {
                x.d("should not process ANR too Fre in %d", new Object[]{10000});
                return;
            }

            this.b = var2;
            this.a.set(1);

            Map var13;
            try {
                var13 = z.a(com.tencent.bugly.crashreport.crash.c.f, false);
            } catch (Throwable var9) {
                x.a(var9);
                x.e("get all thread stack fail!", new Object[0]);
                return;
            }

            if (var13 == null || var13.size() <= 0) {
                x.d("can't get all thread skip this anr", new Object[0]);
                return;
            }

            this.m = this.a(this.c, 10000L);
            if (this.m == null) {
                x.c("proc state is unvisiable!", new Object[0]);
                return;
            }

            if (this.m.pid != Process.myPid()) {
                x.c("not mind proc!", new Object[]{this.m.processName});
                return;
            }

            x.a("found visiable anr , start to process!", new Object[0]);
            this.a(this.c, var1, this.m, var2, var13);
        } catch (Throwable var10) {
            if (!x.a(var10)) {
                var10.printStackTrace();
            }

            x.e("handle anr error %s", new Object[]{var10.getClass().toString()});
            return;
        } finally {
            this.a.set(0);
        }

    }

    private synchronized void f() {
        if (this.h()) {
            x.d("start when started!", new Object[0]);
        } else {
            this.i = new FileObserver("/data/anr/", 8) {
                public final void onEvent(int var1, String var2) {
                    if (var2 != null) {
                        String var3;
                        if (!(var3 = "/data/anr/" + var2).contains("trace")) {
                            x.d("not anr file %s", new Object[]{var3});
                        } else {
                            b.this.a(var3);
                        }
                    }
                }
            };

            try {
                this.i.startWatching();
                x.a("start anr monitor!", new Object[0]);
                this.e.a(new Runnable() {
                    public final void run() {
                        b.this.b();
                    }
                });
            } catch (Throwable var2) {
                this.i = null;
                x.d("start anr monitor failed!", new Object[0]);
                if (!x.a(var2)) {
                    var2.printStackTrace();
                }

            }
        }
    }

    private synchronized void g() {
        if (!this.h()) {
            x.d("close when closed!", new Object[0]);
        } else {
            try {
                this.i.stopWatching();
                this.i = null;
                x.d("close anr monitor!", new Object[0]);
            } catch (Throwable var2) {
                x.d("stop anr monitor failed!", new Object[0]);
                if (!x.a(var2)) {
                    var2.printStackTrace();
                }

            }
        }
    }

    private synchronized boolean h() {
        return this.i != null;
    }

    private synchronized void b(boolean var1) {
        if (var1) {
            this.f();
        } else {
            this.g();
        }
    }

    private synchronized boolean i() {
        return this.j;
    }

    private synchronized void c(boolean var1) {
        if (this.j != var1) {
            x.a("user change anr %b", new Object[]{var1});
            this.j = var1;
        }

    }

    public final void a(boolean var1) {
        this.c(var1);
        var1 = this.i();
        com.tencent.bugly.crashreport.common.strategy.a var2;
        if ((var2 = com.tencent.bugly.crashreport.common.strategy.a.a()) != null) {
            var1 = var1 && var2.c().g;
        }

        if (var1 != this.h()) {
            x.a("anr changed to %b", new Object[]{var1});
            this.b(var1);
        }

    }

    protected final void b() {
        long var1 = z.b() - com.tencent.bugly.crashreport.crash.c.g;
        File var3;
        if ((var3 = new File(this.g)).exists() && var3.isDirectory()) {
            try {
                File[] var19;
                if ((var19 = var3.listFiles()) != null && var19.length != 0) {
                    String var4 = "bugly_trace_";
                    String var5 = ".txt";
                    int var6 = var4.length();
                    int var7 = 0;
                    int var8 = (var19 = var19).length;

                    for(int var9 = 0; var9 < var8; ++var9) {
                        File var10;
                        String var11;
                        if ((var11 = (var10 = var19[var9]).getName()).startsWith(var4)) {
                            try {
                                int var12;
                                if ((var12 = var11.indexOf(var5)) > 0 && Long.parseLong(var11.substring(var6, var12)) >= var1) {
                                    continue;
                                }
                            } catch (Throwable var17) {
                                x.c("Trace file that has invalid format: " + var11, new Object[0]);
                            }

                            if (var10.delete()) {
                                ++var7;
                            }
                        }
                    }

                    x.c("Number of overdue trace files that has deleted: " + var7, new Object[0]);
                    return;
                }

                return;
            } catch (Throwable var18) {
                x.a(var18);
            }
        }

    }

    public final synchronized void c() {
        x.d("customer decides whether to open or close.", new Object[0]);
    }

    public final boolean a(aa var1) {
        Object var2 = new HashMap();
        boolean var3 = false;
        if (var1.e().equals(Looper.getMainLooper())) {
            var3 = true;
        }

        if (var3) {
            this.m = this.a(this.c, 10000L);
            if (this.m == null) {
                x.c("anr handler onThreadBlock proc state is unvisiable!", new Object[0]);
                return false;
            }

            if (this.m.pid != Process.myPid()) {
                x.c("onThreadBlock not mind proc!", new Object[]{this.m.processName});
                return false;
            }

            try {
                var2 = z.a(200000, false);
            } catch (Throwable var4) {
                x.b(var4);
                ((Map)var2).put("main", var4.getMessage());
            }

            x.c("onThreadBlock found visiable anr , start to process!", new Object[0]);
            this.a(this.c, "", this.m, System.currentTimeMillis(), (Map)var2);
        } else {
            x.c("anr handler onThreadBlock only care main thread ,current thread is: %s", new Object[]{var1.d()});
        }

        return true;
    }

    public final boolean d() {
        if (this.k != null && this.k.isAlive()) {
            return false;
        } else {
            this.k = new ab();
            this.k.setName("Bugly-ThreadMonitor" + this.l++);
            this.k.a();
            this.k.a(this);
            return this.k.d();
        }
    }

    public final boolean e() {
        boolean var1 = false;
        if (this.k != null) {
            var1 = this.k.c();
            this.k.b();
            this.k.b(this);
            this.k = null;
        }

        return var1;
    }
}
